
package com.px.pa.modulars.news.contraller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;

import com.px.basic.alone.core.base.BaseController;
import com.px.basic.alone.security.util.SecurityUtils;
import com.px.pa.constants.Constants;
import com.px.pa.modulars.news.entity.CircleNewsColumn;
import com.px.pa.modulars.news.service.CircleNewsColumnService;
import com.px.pa.modulars.upms.entity.SysUser;
import com.px.pa.modulars.upms.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 栏目
 *
 * @author 品讯科技
 * @date 2024-08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/circlenewscolumn")
@Api(value = "circlenewscolumn", tags = "栏目管理")
public class CircleNewsColumnController extends BaseController<CircleNewsColumn, CircleNewsColumnService> {

    private final  CircleNewsColumnService circleNewsColumnService;

    @Autowired
    private SysUserService sysUserService;
    private SysUser getUser(){

        System.out.println(SecurityUtils.getUser());
        SysUser user = sysUserService.getById(SecurityUtils.getUser().getId());
        return user;
    }

    /**
     * 分页查询
     * @param page 分页对象
     * @param circleNewsColumn 栏目
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('circleNewsColumn_circlenewscolumn_get')")
    public R getCircleNewsColumnPage(Page page, CircleNewsColumn circleNewsColumn) {

     Page<CircleNewsColumn> circleNewsColumnPage =  circleNewsColumnService.lambdaQuery()
                .eq(StringUtils.isNotEmpty(circleNewsColumn.getColumnName()),CircleNewsColumn::getColumnName,circleNewsColumn)
                .eq(CircleNewsColumn::getTenantCode,getUser().getOnlyLogo()).eq(CircleNewsColumn::getEnable,Constants.ENABLE_TRUE)
                .page(page);

        return R.ok(circleNewsColumnPage);
    }


    /**
     * 通过id查询栏目
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('circleNewsColumn_circlenewscolumn_get')")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(circleNewsColumnService.getById(id));
    }

    /**
     * 新增栏目
     * @param circleNewsColumn 栏目
     * @return R
     */
    @ApiOperation(value = "新增栏目", notes = "新增栏目")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('circleNewsColumn_circlenewscolumn_add')")
    public R save(@RequestBody CircleNewsColumn circleNewsColumn) {

        circleNewsColumn.setCreateTime(LocalDateTime.now());
        circleNewsColumn.setTenantCode(getUser().getOnlyLogo());
        circleNewsColumn.setCreateBy(getUser().getUserId());
        circleNewsColumn.setEnable(Constants.ENABLE_TRUE);
        circleNewsColumnService.save(circleNewsColumn);
        return R.ok();
    }

    /**
     * 修改栏目
     * @param circleNewsColumn 栏目
     * @return R
     */
    @ApiOperation(value = "修改栏目", notes = "修改栏目")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('circleNewsColumn_circlenewscolumn_edit')")
    public R updateById(@Validated @RequestBody CircleNewsColumn circleNewsColumn) {
        circleNewsColumn.setUpdateTime(LocalDateTime.now());
        circleNewsColumn.setUpdateBy(getUser().getUserId());
        return R.ok(circleNewsColumnService.updateById(circleNewsColumn));
    }

    /**
     * 通过id删除栏目
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除栏目", notes = "通过id删除栏目")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('circleNewsColumn_circlenewscolumn_del')")
    public R removeById(@PathVariable Integer id) {
        CircleNewsColumn circleNewsColumn=circleNewsColumnService.getById(id);
        if (circleNewsColumn ==null){
            return R.failed("ID错误");
        }
        circleNewsColumn.setEnable(Constants.ENABLE_FALSE);
        return R.ok(circleNewsColumnService.updateById(circleNewsColumn));
    }

    @ApiOperation(value = "栏目列表", notes = "栏目列表")
    @GetMapping("/list")
    public R getList(){
      List<CircleNewsColumn> circleNewsColumnList =  circleNewsColumnService.lambdaQuery().eq(CircleNewsColumn::getEnable,Constants.ENABLE_TRUE)
                .orderByDesc(CircleNewsColumn::getCreateTime).list();
      return R.ok(circleNewsColumnList);
    }

}
